using LS.Localization;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEditorInternal;
namespace LS.Editor
{
    public class LanguageSettingGUIHolder: System.IDisposable
    {
        public bool showScriptFiled = false;
        public bool showSourceFiled = true;
        public bool hostModifiedOperate = true;
        private static readonly Type m_TargetType = typeof(LanguageSetting);
        private static readonly Type m_TextAssetType = typeof(TextAsset);

        private SerializedObject m_SerializedObject;
        private SerializedProperty m_SerializedProperty;
        private SerializedProperty m_Script;
        private SerializedProperty m_Data;
        private SerializedProperty m_Language;
        private ReorderableList m_List;

        private bool m_ListContentChangedDirty = false;

        class Style
        {
            public readonly static GUIContent ListHeader = new GUIContent("Datas");
            public readonly static GUIContent SrouceContent = new GUIContent("Source");
        }
        public LanguageSettingGUIHolder(SerializedObject serializedObject,bool hostModifiedOperate = true)
        {
            this.hostModifiedOperate = hostModifiedOperate;
            LanguageSetting setting = serializedObject.targetObject as LanguageSetting;
            m_SerializedObject = serializedObject;
            m_Data = m_SerializedObject.FindProperty("m_Datas");
            m_Script = m_SerializedObject.FindProperty("m_Script");
            m_Language = m_SerializedObject.FindProperty("m_Language");
            m_List = new ReorderableList(serializedObject, m_Data);
            
            m_List.drawHeaderCallback = rect =>
            {
                EditorGUI.LabelField(rect, Style.ListHeader);
            };

            m_List.elementHeightCallback = index =>
            {
                return EditorGUIUtility.singleLineHeight;
            };

            m_List.drawElementCallback = (rect, index, active, focused) =>
            {
                var content = m_Data.GetArrayElementAtIndex(index);
                EditorGUI.PropertyField(rect, content);
            };

            m_List.onChangedCallback = (ReorderableList list) =>
            {
                Debug.Log("onChangedCallback");
                m_ListContentChangedDirty = true;
            };
        }

        public LanguageSettingGUIHolder(LanguageSetting serializedObject, bool hostModifiedOperate = true) :this(new SerializedObject(serializedObject), hostModifiedOperate)
        {
        
        }

        ~LanguageSettingGUIHolder()
        {
            Dispose();
        }

        public void DrawGui()
        {
            if (hostModifiedOperate)
            {
                this.m_SerializedObject.UpdateIfRequiredOrScript();
            }
            using (new EditorGUI.DisabledScope(true))
            {
            
                if (showScriptFiled)
                    EditorGUILayout.PropertyField(m_Script);

                if (showSourceFiled)
                    EditorGUILayout.ObjectField(Style.SrouceContent,m_SerializedObject.targetObject, m_TargetType, false);

                EditorGUILayout.PropertyField(m_Language);
            }
            m_ListContentChangedDirty = false;
            m_List.DoLayoutList();
            if (hostModifiedOperate)
            {
                this.m_SerializedObject.ApplyModifiedProperties();
            }

            if (m_ListContentChangedDirty)
            {
                EditorUtility.SetDirty(m_SerializedObject.targetObject);
            }
        }

        public void Dispose()
        {
            //m_InnerList.Clear();
            m_List = null;
            m_SerializedObject = null;
            m_Script = null;
            m_Data = null;
        }
    }
}
